#!/usr/bin/python
# -*- coding: UTF-8 -*-

################################################################################
#
# Copyright (c) 2020 openEuler.org, Inc. All Rights Reserved
#
################################################################################
"""
Control task

Authors: xiaojianghui
update by: zhangjianjun
Date:    10/22/2020 11:01 AM
"""

from tabletask import (runtask, mappingtask, toexcel, export_excel_task,
                       import_excel_task, package_committer_task,
                       spec_error_task, repeattask, gauss_yaml,
                       issue_statistics, supplement_cve, mindspore_yaml,
                       openlookeng_yaml)
from gitwebtask import genegroup, yamltask
from emailtask import sendingtask, issue_record_email
from deletetask import deletefiletask
import os
import threading

mutex_lock = threading.Lock()

def toexcelcontroller():
    """
    Query the data whose cve_status status is 3 or 4 in the
    database to generate excel Modify the status of cve_status to 3 or 4 to 5,
    indicating that it has been added to excel to be processed

    Returns:
        return None
    """
    print("Export data to generate excel, "
          "send error data feedback email task start")
    toexcel.MysqlToExcel().generate_table()
    toexcel.MysqlToExcel().update_status()
    sendingtask.sending()
    print("To excel and send email task completed")


def runtabletask():
    """
    Parse the artificial CVE table, crawl the CVE official
    website data, and store it in the database
    return None
    """
    print("Analyze the manual data table, "
          "crawl the CVE official website data, and start the timing task")
    spec_error_task.add_error_details()
    runtask.handle_data()
    repeattask.repeat_task()
    repeattask.get_published_date_task()
    print("Analyze the manual data table, "
          "crawl the CVE official website data task completed")


def runmappeingtask():
    """
    Parse the artificial CVE table, crawl the CVE
    official website data, and store it in the database
    return None
    """
    print("The timing task of parsing the "
          "package name mapping table starts")
    mappingtask.to_mysql()
    print("The timing task of parsing the "
          "package name mapping table completed")


def gwcontroller():
    """
    Grab warehouse yaml data
    return None
    """
    print("Start of crawling organization, warehouse, "
          "owners correspondence relationship timing task")
    genegroup.Command().handle()
    yamltask.Task().download()
    yamltask.Task().read()
    print("Grab warehouse yaml data task completed")


def rundelfiletask():
    """
    Delete files older than one month
    return None
    """

    print("Clean up the folder timed task starts")
    deletefiletask.deldir("./oldexcels")
    print("Clean up the folder task completed")


def run_issue_record_task():
    """
    issues record email task
    return None
    """
    print('issues record email task start')
    for filex in os.listdir('./export_excels'):
        os.remove('./export_excels/' + filex)
    export_excel_task.generate_excels(1, './export_excels/create_issues')
    export_excel_task.generate_excels(2, './export_excels/unctreated_issues')
    issue_record_email.sending()
    print('issues record email task complete')


def run_whiltlist_task():
    """
    the task of parsing package whitelist
    return None
    """
    print('the task of parsing package whitelist start')
    import_excel_task.import_data()
    print('the task of parsing package whitelist complete')


def run_package_committer_task():
    """
    the task of parsing package_committer excels
    Returns:None

    """
    print('the task of parsing package_committer_excels start')
    package_committer_task.import_data()
    print('the task of parsing package_committer_excels complete')


def parse_opengauss_yaml_task():
    """
        Download the yaml file first, then parse the yaml file of opengauss
    Returns:None

    """
    print('the task of Process opengauss yaml data start')
    gauss_yaml.proc_gauss_yaml()
    print('the task of Process opengauss yaml data complete')


def issue_statistics_email_task():
    """
    Parse the issue statistics recipient list
    return None
    """
    print('Parse the issue statistics recipient list start')
    issue_statistics.get_issue_excel()
    print('Parse the issue statistics recipient list complete')


def supplement_cve_task():
    """
    Complete the template information of the
    issue with the data on the CVE official website
    return None
    """
    print("Supplemental cve information task starts")
    mutex_lock.acquire()
    supplement_cve.supplement_cve()
    mutex_lock.release()
    print("Supplement cve information task is over")


def long_supplement_cve_task():
    """
    Complete the template information of the
    issue with the data on the CVE official website
    return None
    """
    print("long Supplemental cve information task starts")
    supplement_cve.long_supplement_cve()
    print("long Supplement cve information task is over")


def parse_mindspore_yaml_task():
    """
    Download the yaml file first, then parse the yaml file of mindspore
    Returns:None
    """
    print('the task of Process mindspore yaml data start')
    mindspore_yaml.proc_mindspore_yaml()
    print('the task of Process mindspore yaml data complete')


def parse_openlookeng_yaml_task():
    """
    Download the yaml file first, then parse the yaml file of openLooKeng
    Returns:None
    """
    print('the task of Process openLooKeng yaml data start')
    openlookeng_yaml.proc_openlookeng_yaml()
    print('the task of Process openLooKeng yaml data complete')
